package me.shan.leet.code;

import java.util.HashSet;
import java.util.Set;

public class T1079LetterTilePossibilities {

    public int numTilePossibilities(String tiles) {
        Set<String> result = new HashSet<>();
        boolean[] visited = new boolean[tiles.length()];
        dfs(tiles, result, visited, new StringBuilder());
        return result.size() - 1;
    }

    private void dfs(String tiles, Set<String> result, boolean[] visited, StringBuilder stringBuilder) {
        result.add(stringBuilder.toString());
        for (int i = 0; i < tiles.length(); i++) {
            if (!visited[i]) {
                visited[i] = true;
                stringBuilder.append(tiles.charAt(i));
                dfs(tiles, result, visited, stringBuilder);
                visited[i] = false;
                stringBuilder.deleteCharAt(stringBuilder.length() - 1);
            }
        }
    }

}
